package com.effectiv.gooruda.rest.api.controller;

import java.util.List;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.effectiv.gooruda.rest.api.domain.FileStorage;

import com.effectiv.gooruda.rest.api.operation.FileStorageOperation;

@Controller
@RequestMapping("/api/file")
@Slf4j
public class FileStorageController implements FileStorageOperation {

	@Qualifier("fileStorageServiceImpl")
	@Autowired
	private FileStorageOperation operation;

	@RequestMapping(value = "/upload/{userName}/{folder}/{description}/{tags}", method = RequestMethod.POST)
	@Override
	public @ResponseBody
	FileStorage store(@PathVariable String userName,
			@PathVariable String folder, @PathVariable String description,
			@PathVariable String tags, @RequestParam MultipartFile file) {

		return operation.store(userName, folder, description, tags, file);
	}

	@RequestMapping(value = "/replace/{fileName}/{folder}/{description}", method = RequestMethod.POST)
	@Override
	public FileStorage update(@PathVariable String fileName,
			@PathVariable String folder, @PathVariable String description,
			@RequestParam MultipartFile file) {

		return operation.update(fileName, folder, description, file);
	}

	@RequestMapping(value = "/addtags/{fileName}/{folder}/{tags}", method = RequestMethod.POST)
	@Override
	public @ResponseBody
	boolean addTags(@PathVariable String fileName, @PathVariable String folder,
			@PathVariable String tags) {

		log.info("tags = {}", tags);

		return operation.addTags(fileName, folder, tags);
	}

	// Tags always added to latest version and this does not create new version,
	// new version is created by physical file update
	@RequestMapping(value = "/removetags/{fileName}/{folder}/{tags}", method = RequestMethod.POST)
	@Override
	public @ResponseBody
	boolean removeTags(@PathVariable String fileName,
			@PathVariable String folder, @PathVariable String tags) {

		return operation.removeTags(fileName, folder, tags);
	}

	@Override
	@RequestMapping(value = "/removetags/{fileName}/{folder}", method = RequestMethod.POST)
	public @ResponseBody boolean removeAllTags(@PathVariable String fileName,
			@PathVariable String folder) {
		return operation.removeAllTags(fileName, folder);
	}

	@Override
	@RequestMapping(value = "/removeowner/{fileName}/{folder}", method = RequestMethod.POST)
	public boolean removeOwner(@PathVariable String fileName, @PathVariable String folder) {
		return operation.removeOwner(fileName, folder);
	}
	
	@RequestMapping(value = "/changeowner/{fileName}/{folder}/{oldOwner}/{newOwner}", method = RequestMethod.POST)
	@Override
	public @ResponseBody boolean changeOwner(@PathVariable String fileName, @PathVariable String folder, @PathVariable String oldOwner,
			@PathVariable String newOwner) {
		return operation.changeOwner(fileName, folder, oldOwner, newOwner);
	}

	@Override
	public @ResponseBody FileStorage moveFolder(@PathVariable String fileName, @PathVariable String oldFolder,
			@PathVariable String newFolder) {
		
		return operation.moveFolder(fileName, oldFolder, newFolder);
	}

	@Override
	public boolean deleteFolder(String folder) {
		//Find all files
		
		//Delete all physical files
		
		//delete all storage files
		return false;
	}

	@Override
	public boolean renameFolder(String oldFolder, String newFolder) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public String deleteByUser(String userName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String deleteByName(String fileName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String deleteAllByUser(String userName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String deleteAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long countByType(String type) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long countByUser(String userName) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<FileStorage> findByType(String type, Pageable page) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<FileStorage> findByOwner(String userName, Pageable page) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<FileStorage> findAll(Pageable page) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<FileStorage> findByTags(String[] tags, Pageable page) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public byte[] download(String fileName) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public FileStorage findByName(String fileName, Pageable page) {
		// TODO Auto-generated method stub
		return null;
	}

}
